/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery(document).ready(function() {
    //-------------------------------------------------------
    /*shows the loading div every time we have an Ajax call*/
    $("#loading").bind("ajaxSend", function(){
        $(this).css({
            'left':(($(window).width()/2) - ($(this).width()/2))
            });
        $(this).show();
    }).bind("ajaxComplete", function(){
        $(this).hide();
         
    });
    //-------------------------------------------------------
    $().ajaxError(function(){
        $("#Ajax_Error").dialog({
            resizable: false,
            height:140,
            modal: true,
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            buttons: {
                'Ok': function() {
                    $(this).dialog('close');
                }
            }
        }).dialog('open');
});
//-------------------------------------------------------
});
;(function($){
    $.simplelogajax = {
    	ajax:function(options){
    		var myOptions = {
    			dataType:'json',
    			type:'POST',
    			error:function (XMLHttpRequest, textStatus, errorThrown) {
                	confirmBox({
                		title:"Form Error",
                		msg:"An error has occur. Please refresh or call the admin for assitance."+textStatus
                	});
    			}
    		};
    		$.extend(myOptions,options);
    		$.ajax(myOptions);
    	},
    	popup:function(url,width,height){
    		if(width == '' || width == null || width == 0)
    			width = 600;
    		if(height == '' || height == null || height == 0)
    			height = 500;
    		window.open(url,url,'resizable=1,scrollbar=1,location=0,toolbar=1,width='+width+',height='+height);
    	},
        notification:function(msg,type){
            if(type==''||type==null)
                type='info';
            var container = '<div id="notification" class="'+type+'">'+msg+'</div>';
            var height = $(window).height();
            $(container).css('top', height);
            $(container).css('left',0);
            $('body').append(container);
            $(container).animate({top:'-='+$(container).height()});

        }
    };
})(jQuery);
;(function($){
    $.riotingNerds = {
    	makeSlider:function(options){
    		var myOptions = {
                    width:'350px',
                    content:'',
                    title:'',
                    contentUrl:'',
                    parentDiv:$('#content')
    		};
    		$.extend(myOptions,options);
    		
    	},
    	popup:function(url,width,height){
    		if(width == '' || width == null || width == 0)
    			width = 600;
    		if(height == '' || height == null || height == 0)
    			height = 500;
    		window.open(url,url,'resizable=1,scrollbar=1,location=0,toolbar=1,width='+width+',height='+height);
    	}
    };
})(jQuery);
;(function($){
    $.fn.getInfo = function(options){
    	var constructor = $(this);
    	myOptions = {
    		url:'',    		
    		dataType:'html',
    		data:{id:$(constructor).val()},
    		type:'GET',
    		beforeSend:function(){
    			var html = '<img id="ajaxloading" src="/images/ajax-loader.gif" alt="Loading.." />';
    			$(constructor).after(html);
    		},
    		complete:function(){
    			if($(constructor).next().attr('id') == 'ajaxloading')
    				$(constructor).next().remove();
    		}
    	};
    	$.extend(myOptions,options)
    	$.simplelogajax.ajax(myOptions);
    };
})(jQuery);
;(function($){
	$.simplelogdialog_counter = 0;
        
    $.simplelogdialog = function(options,dialogOptions,dialogButton) {
    	var dialogID = "SimpleLog_dialog_"+$.simplelogdialog_counter;
    	var dialogContainer = "<div id='"+dialogID+"' style='display:none'></div>";
    	$.simplelogdialog_counter++;
    	$('body').append(dialogContainer);
    	var myDialogButton = {
        	'Cancel':function(){
    			$(this).dialog('close');
    		}
        };
    	$.extend(myDialogButton,dialogButton);
    	var myDialogOptions = {
    		autoOpen: true,
    		show: 'drop',
    		stack: false,
    		hide: 'drop',
    		buttons:myDialogButton,
    		close:function(){
    			$('#'+dialogID).remove();
    			$(this).dialog('distory');
    		}
    	};
    	$.extend(myDialogOptions,dialogOptions);
    	var myOptions = {
    		show:false    		
    	};
    	var defaultAfterShow = function(){
    		$('#'+dialogID+' .buttons').hide();
			var title = $('#'+dialogID+' h1').html()||$('#'+dialogID+' h2').html();
			$('#'+dialogID+' h1, #'+dialogID+' h2').hide();
			if(myDialogOptions.title == null && title == '')
				$('#'+dialogID).parent().find('.ui-dialog-titlebar').hide();
			if(myDialogOptions.title == null)
				myDialogOptions.title = title;
			$('#'+dialogID).dialog(myDialogOptions);
    	};
    	
    	$.extend(myOptions,options);
    	if(typeof myOptions.afterShow == 'function') {
    		myOptions.afterShow = function(){
    			options.afterShow();
    			defaultAfterShow();
    		}
    	}
    	else
    		myOptions.afterShow = defaultAfterShow;
    	$('#'+dialogID).subcontent(myOptions);
    	};

    $.simplelogdialog.Form = function(options,dialogOptions,dialogButton) {
        var dialogID = "SimpleLog_dialog_"+$.simplelogdialog_counter;
        var dialogContainer = "<div id='"+dialogID+"' style='display:none'></div>";
        $.simplelogdialog_counter++;
        $('body').append(dialogContainer);
        var myDialogButton = {
            'Create':function(){
                $.simplelogform.loadingButtons['dialogSubmit'] = $(this).parent().find('.ui-dialog-buttonpane').children('.ui-dialog-buttonset')
                $(this).find('form').find('[type=submit]').click();
                //$('#form_loading_button').button().attr('disabled','disabled').addClass('ui-dialog-buttonset');
            },
            'Cancel':function(){
                $(this).dialog('close');
            }
        };
        var myDialogOptions = {
            autoOpen: true,
            stack: false,
            modal:true,
            width:550,
            height:500,
            buttons:myDialogButton,
            close:function(){
                $('#'+dialogID).remove();
                $(this).dialog('distory');
            }
        };
        $.extend(myDialogOptions,dialogOptions);
        var myOptions = {
            show:false
        };
        var defaultAfterShow = function(){
            $('#'+dialogID+' .buttons').hide();
            var title = $('#'+dialogID+' h1').html()||$('#'+dialogID+' h2').html();
            $('#'+dialogID+' h1, #'+dialogID+' h2').hide();
            if(myDialogOptions.title == null && title == '')
                $('#'+dialogID).parent().find('.ui-dialog-titlebar').hide();
            else
                $('#ui-dialog-title-'+dialogID).html(title);
        };
        
        $.extend(myOptions,options);
        if(typeof myOptions.afterShow == 'function') {
            myOptions.afterShow = function(){
                options.afterShow();
                defaultAfterShow();
            }
        }
        else
            myOptions.afterShow = defaultAfterShow;
        $('#'+dialogID).dialog(myDialogOptions);
        $('#'+dialogID).subcontent(myOptions);
    };
    $.simplelogdialog.Loading = {
    	Open:function(msg){
    		var dialogContainer = "<div id='SimpleLog_Loading' style='display:none'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>"+msg+"</p></div>";
    		$('body').append(dialogContainer);
    		var myDialogOptions={
    			modal: true,
    			resizable:false,
    			draggable:false,
    			closeOnEscape:false,
    			closeText:''
    		};
    		$('#SimpleLog_Loading').dialog(myDialogOptions);
    		$('#SimpleLog_Loading').parent().find('.ui-dialog-titlebar').hide();
    	},
    	Close:function(){
    		$('#SimpleLog_Loading').dialog('close');
    		$('#SimpleLog_Loading').remove();
    	}
    };
    $.simplelogdialog.confirm = function(msg,dialogOptions){
    	var res = false;
    	var buttons = {
    		'Yes':function(){
    			res = true;
    			$(this).dialog('close');
    		},
    		'No':function(){
    			res = false;
    			$(this).dialog('close');
    		}
    	};
    	var myDialogOptions = {
        		autoOpen: true,
        		show: 'drop',
        		stack: false,
        		hide: 'drop',
        		buttons:buttons,
        		close:function(){
        			$('#SimpleLog_confirm').remove();
        			$(this).dialog('distory');
        		},
        		open:function(){
        			$('#SimpleLog_confirm').parent().find('.ui-dialog-titlebar').hide();
        			return this;
        		}
        	};
    	var dialogContainer = "<div id='SimpleLog_confirm' style='display:none'><p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>"+msg+"</p></div>";
    	$('body').append(dialogContainer);
    	$('#SimpleLog_confirm').dialog(myDialogOptions);
    	return res;
    };
})(jQuery);

;(function($){
    $.fn.subcontent = function(options) {
        var myoptions = {
            valuetofield: {},
            beforeShow:function(){return true;},
            afterShow:function() {},
            url:'',
            data:{},
            createContainer:false,
            container:$('body'),
            show:{
            	method:'prepend',
            	effect:'slide',
            	effectOptions:{
            		direction:"up"
            	},
            	timeOut:1000
            }
        };
        $.extend(myoptions, options);

        var container = $(this);
        if(myoptions.createContainer){
            $(this).append("<div id='subContainer'></div>");
            container = $(this).children('#subContainer');
        }
        var loadingDiv = '<div id="simplelogsubcontent_loading" align="center"><img src="/images/container-loader.gif" alt="Loading.." /></div>';
        if(!myoptions.beforeShow())
            return false;
        
        $.extend(myoptions.data,{ajax:'ajaxcontent'});
        $.simplelogajax.ajax({
            data:myoptions.data,
            dataType:'html',
            type:'GET',
            beforeSend:function(){
        		$(container).html(loadingDiv);
            },
            url:myoptions.url,
            success:function(data,status){
            	if(myoptions.show) {
            		olddata = data;
            		//data = "<div class='subcontentItem'>"+data+"</div>";
            		switch(myoptions.show.method){
            			case 'replaceWith':$(container).replaceWith(data);
            				break;
            			case 'html':$(container).html(data);
            				break;
            			case 'append':$(container).append(data);
            				break;
            			default:$(container).prepend(data);
            				break;
            		}
                        myoptions.afterShow();
            		//$('.subcontentItem').slideDown('1000',function(){$(this).replaceWith(olddata);myoptions.afterShow();});
            	}
            	else{
            		$(container).html(data);
            		myoptions.afterShow();
            	}
            },
            complete:function(){$('#simplelogsubcontent_loading').remove();}
        });
    };
})(jQuery);

;(function($){
    $.simplelogcontent = {

    	mainContent : '.span-19',
    	
        valuetofield:{},
        
        setFields:function() {
            $.extend(this.valuetofield,{});
            for(key in this.valuetofield) {
                $('#'+key).val(this.valuetofield[key]);
            }
        },

        beforeShow:function() {
            return true;
        },
        
        afterShow:function(containerID) {},
        
        show:function(urls) {
            if(!this.beforeShow())
                return;
            var maincontainer = $(this.mainContent);
            var constructor = this;
            var displayID = 'div#content';
            var backlink = '<a href="#" onclick="$.simplelogcontent.backstep();">Back</a>';
            var loadcontent = $('<div id="content" class="subcontentItem"></div>').appendTo(maincontainer);
            $(loadcontent).append(backlink);
                $.simplelogajax.ajax({
                    url:urls,
                    type:'GET',
                    dataType:'html',
                    success:function(data,status){
                        $(maincontainer).children('div#content').hide();
                        $(loadcontent).append(data).appendTo(maincontainer);
                        $(loadcontent).show('slide',{direction:'right'},500);
                        constructor.afterShow(loadcontent);
                    }
                });
        },
        
        backstep: function() {
        	//$(this.mainContent).find('#content').hide();
        	var content = $(this.mainContent).find('#content:last');        	
        	//$(content).prev('#content').show('slide',{direction:'left'},500);
        	$(content).prev('#content').show();
        	$(content).remove();
        	return false;
        },
        waitingDialog: function(waiting) { // I choose to allow my loading screen dialog to be customizable, you don't have to
            if($("#loadingScreen").length <=0)
                $('body').append('<div style="display:none;" id="loadingScreen"></div>');

            $("#loadingScreen").html(waiting.message && '' != waiting.message ? waiting.message : 'Please wait...');
            $("#loadingScreen").dialog({
                autoOpen: false,	// set this to false so we can manually open it
                dialogClass: "loadingScreenWindow",
                closeOnEscape: false,
                draggable: false,
                width: 460,
                minHeight: 50,
                modal: true,
                buttons: {},
                resizable: false,
                open: function() {
                    // scrollbar fix for IE
                    $('body').css('overflow','hidden');
                },
                close: function() {
                    // reset overflow
                    $('body').css('overflow','auto');
                }
            });
            $("#loadingScreen").dialog('option', 'title', waiting.title && '' != waiting.title ? waiting.title : 'Loading');
            $("#loadingScreen").dialog('open');
        },
        closeWaitingDialog: function() {
            $("#loadingScreen").dialog('close');
        //$("#loadingScreen").dialog('destory');
        }

    };
})(jQuery);

;(function($){
    $.simplelogcrumbs = {
    	Data:$('body').data('breadcrumbs'),
    	
    	getLabel:function(url){
    		urls = urls.replace('.html','');
    		urlarray = urls.split('/');
    		g=0;
    		for(var x=0;x<urlarray.length;x++) {
    			if(urlarray[x] == 'index.php')
    				g = x+1;
    		}
    	},
    	Add:function(url){
    		
    	},
    	Remove:function(index){
    	},
    	MoveTo:function(index){
    	},
    	Back:function(){
    	},
    	Forward:function(){
    	}
    };
})(jQuery);

;(function($){
    $.simplelogform = {

        loadingButtons:[],

        formObj: $('form'),
    
        beforeSubmit:function() {
            return true;
        },
    
        Submit:function(formObj,params) {
            var constructor = this;
            if(typeof formObj != "undefined")
                $.simplelogform.formObj = formObj;
            
            //add new params
            $($.simplelogform.formObj).append("<div id='form_params'></div>");
            for(var key in params) {
                var input = "<input type='hidden' name='"+key+"' value='"+params[key]+"' />";
                $($.simplelogform.formObj).find("#form_params").append(input);
            }
            $.simplelogform.loadingButtons['formSubmit'] = $($.simplelogform.formObj).find('input[type=submit]');
            var submitButton = $.simplelogform.loadingButtons;
            $('div.errorMessage').remove();
            if($.simplelogform.beforeSubmit()) {
                $.simplelogajax.ajax({
                    url:$($.simplelogform.formObj).attr('action'),
                    dataType:'json',
                    data:$($.simplelogform.formObj).serialize()+"&ajax="+$($.simplelogform.formObj).attr('id'),
                    type:'POST',
                    success:function(data,status){
                        if(data == '' || data == null){
                            constructor.afterSubmit(data);
                            return;
                        }
                        var returnData = {
                        	attr:'',
                        	formerror:'',
                        	summary:'',
                        	id:''
                        };
                        $.extend(returnData,data); 
                        if(returnData.attr != '')
                            constructor.failValidation(returnData.attr,data['summary']);
                        else if(returnData.formerror != '') {
                            confirmBox({
                                title:'Saving Error!',
                                msg:returnData.formerror
                            });
                        } 
                        else {
                        	constructor.afterSubmit(returnData);                            
                        }
                        return false;
                    },
                    global:false,
                    error:function (XMLHttpRequest, textStatus, errorThrown) {
                        confirmBox({
                            title:"Form Error",
                            msg:"An error has occur. Please refresh or call the admin for assitance."+textStatus
                        });
                    },
                    complete:function(){
                        
                    	$('button#form_loading_button').remove();
                        for(var key in submitButton)
                            $(submitButton[key]).show();
                        
                    },
                    beforeSend:function(){
                        $(formObj).find('input, textarea').removeClass('error');
                        $(formObj).find('#errorsummary').empty();
                      //make loading button
                      var loadingButton = "<button id='form_loading_button'><img src='/images/ajax-loader-button.gif' alt='Loading Gif' />  Validating and Saving...</button>";
                        for(var key in submitButton) {
                            $(submitButton[key]).hide();
                            $(submitButton[key]).after(loadingButton);
                        }
                        
                        $('button#form_loading_button').button().attr('disabled','disabled');
                        
                    }
                });
            }
            
            $($.simplelogform.formObj).find("#form_params").remove();
        },

        afterSubmit:function(data) {
            $.simplelogcontent.show($.simplelogcontent.backstepBreadCrumbs());
        },

        failValidation:function(Obj,Summary){
            for(var key in Obj){
            	var container = $(this.formObj).find('#'+key);
                $(container).addClass('error');
                if($(container).is('textbox, :text, select')) {
                	var err = '<div class="errorMessage">'+Obj[key]+'</div>';
                	$(container).parent().append(err);
                }
            }
            $('#errorsummary').html(Summary);
        }
    };
})(jQuery);